package handle

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

var conf map[string]*string

type Config struct {
	Mysql struct {
		Host     string `yaml:"host"`
		Port     string `yaml:"port"`
		Username string `yaml:"username"`
		Password string `yaml:"password"`
		DbName   string `yaml:"db"`
	}
	File struct {
		Target string `yaml:"target"`
	}
}

func ReadYaml(path *string) {
	reader, err := ioutil.ReadFile(*path)
	if err != nil {
		log.Fatal(err)
	}
	config := &Config{}
	err = yaml.Unmarshal(reader, config)
	if err != nil {
		log.Fatal(err)
	}
	println()
	log.Printf("| mysql ")
	log.Printf("| host     | %s", config.Mysql.Host)
	log.Printf("| port     | %s", config.Mysql.Port)
	log.Printf("| username | %s", config.Mysql.Username)
	log.Printf("| password | %s", config.Mysql.Password)
	log.Printf("| dbname   | %s", config.Mysql.DbName)
	log.Printf("| file ")
	log.Printf("| target   | %s ", config.File.Target)
	println()

	url := config.Mysql.Username + ":" + config.Mysql.Password + "!@tcp(" + config.Mysql.Host + ":" + config.Mysql.Port + ")/" + config.Mysql.DbName + "?allowNativePasswords=true&parseTime=true"
	conf = make(map[string]*string)
	conf["url"] = &url
	conf["dbname"] = &config.Mysql.DbName
	conf["target"] = &config.File.Target
	log.Printf("| mysql connect | %s", *conf["url"])
	log.Printf("| target file   | %s", *conf["target"])

}
